﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using StudentLookup.BaseEntity;
using System.Net.Http;
using HtmlAgilityPack;
using StudentLookup.Utilities;

namespace StudentLookup.UserControl
{
    public partial class ResultTestDetail : PhoneApplicationPage
    {
        public ResultTestDetail()
        {
            InitializeComponent();
        }

        protected async override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            string indexSelected = "";
            int index;
            string studentId = "";
            Student student = new Student();
            if (NavigationContext.QueryString.TryGetValue("studentId", out studentId))
            {
                student.StudentId = studentId;
            }

            string htmlString = "";
            using (var client = new HttpClient())
            {
                htmlString = await client.GetStringAsync(Constant.URL_ResultTest + student.StudentId);
            }
            try
            {
                HtmlDocument htmlDocument = new HtmlDocument();
                htmlDocument.LoadHtml(htmlString);
                List<ResultTest> listResultTest = new List<ResultTest>();
                foreach (var item in htmlDocument.DocumentNode.SelectNodes("//tr[@class='kTableRow'] | //tr[@class='kTableAltRow']"))
                {
                    ResultTest resultTest = new ResultTest();
                    resultTest.CourseName = item.SelectSingleNode(".//td[2]/a").InnerText.Trim();
                    resultTest.ClassIndependentId = item.SelectSingleNode(".//td[3]/a").InnerText.Trim();
                    resultTest.TestScoreFirst = item.SelectSingleNode(".//td[4]").InnerText.Trim();
                    resultTest.TestScoreSecond = item.SelectSingleNode(".//td[5]").InnerText.Trim();
                    resultTest.TestScoreThird = item.SelectSingleNode(".//td[6]").InnerText.Trim();
                    resultTest.TestScoreFourth = item.SelectSingleNode(".//td[7]").InnerText.Trim();
                    resultTest.TestScoreFifth = item.SelectSingleNode(".//td[8]").InnerText.Trim();
                    resultTest.TestScoreSixth = item.SelectSingleNode(".//td[9]").InnerText.Trim();
                    resultTest.ResultTestMidCourse = item.SelectSingleNode(".//td[10]").InnerText.Trim();
                    resultTest.ResultTestAverage = item.SelectSingleNode(".//td[12]/strong").InnerText.Trim();
                    resultTest.HourAbsent = item.SelectSingleNode(".//td[13]").InnerText.Trim();
                    resultTest.ExamCondition = item.SelectSingleNode(".//td[14]").InnerText.Trim();

                    listResultTest.Add(resultTest);
                }
                if (NavigationContext.QueryString.TryGetValue("indexSelected", out indexSelected))
                {
                    index = int.Parse(indexSelected);
                    txtCourseNameRT.Text = Library.LimitWords(listResultTest[index].CourseName, 12, " ...");
                    txtClassIndepentId.Text = listResultTest[index].ClassIndependentId;
                    txtTest1.Text = listResultTest[index].TestScoreFirst;
                    txtTest2.Text = listResultTest[index].TestScoreSecond;
                    txtTest3.Text = listResultTest[index].TestScoreThird;
                    txtResultMidle.Text = listResultTest[index].ResultTestMidCourse;
                    txtResultAverage.Text = listResultTest[index].ResultTestAverage;
                    txtHourAbsent.Text = listResultTest[index].HourAbsent;
                    txtConditionExam.Text = listResultTest[index].ExamCondition;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}